package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.qo.EmployeeQueryObject;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.PageResult;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IRoleService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import net.sf.jsqlparser.schema.MultiPartName;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.Security;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IDepartmentService departmentService;

    @Autowired
    private IRoleService roleService;


    @RequiresPermissions(value = {"employee:list","员工列表"},logical= Logical.OR)
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") EmployeeQueryObject qo) {
        PageInfo<Employee> pageResult = employeeService.query(qo);
        model.addAttribute("pageResult", pageResult);

        List<Department> departments = departmentService.listAll();


        model.addAttribute("departments", departments);


        //WEB-INF/views/employee/list.ftl
        return "employee/list";
    }

    //处理删除请求
    @RequiresPermissions(value = {"employee:delete","员工删除"},logical= Logical.OR)
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
            employeeService.delete(id);
        }
        return "redirect:/employee/list.do";
    }

    //处理跳转保存请求

    @RequestMapping("/input")
    public String input(Long id, Model model) {
        //解决了跳转页面中不能下拉框回显的数据查询
        List<Department> departments = departmentService.listAll();
        model.addAttribute("departments", departments);
        model.addAttribute("roles", roleService.listAll());

        if (id != null) {
            model.addAttribute("employee", employeeService.get(id));
        }
        return "employee/input";
    }

    //添加员工和编辑员工
    @RequiresPermissions(value = {"employee:saveOrUpdate","员工添加或编辑"},logical= Logical.OR)
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Employee employee, Long[] ids) {

        try {
            if (employee.getId() != null) {//修改
                employeeService.update(employee, ids);
            } else {//保存
                employeeService.save(employee, ids);
            }
            return new JsonResult();

        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false, "保存失败");
        }
    }

    //批量删除
    @RequiresPermissions(value = {"employee:batchDelete","员工批量删除"},logical= Logical.OR)
    @RequestMapping("/batchDelete")
    @ResponseBody
    public JsonResult batchDelete(Long[] ids) {
        try {
            if (ids != null && ids.length > 0) {
                employeeService.batchDelete(ids);

            }
            return new JsonResult();
        } catch (Exception e) {
            return new JsonResult(false, "删除失败");
        }
    }


    //检查用户名是否存在
    @RequestMapping("/checkName")
    @ResponseBody
    public Map<String, Boolean> checkName(String name) {  // valid:true
        Employee employee = employeeService.selectByName(name);
        HashMap<String, Boolean> map = new HashMap<>();
        map.put("valid", employee == null);
        return map;
    }

    /**
     * 文件导出
     *
     * @param response
     * @throws IOException
     */
    @RequestMapping("/exportxls")
    public void exportxls(HttpServletResponse response) throws IOException {
        //文件下载响应头
        response.setHeader("Content-Disposition", "attachment;filename=employee.xls");
        //创建excel文件
        Workbook wb = employeeService.exportxls();
        //把excel的数据输出给浏览器
        wb.write(response.getOutputStream());
    }

    /**
     * 导入文件
     * @param file
     * @return
     * @throws IOException
     */
    @RequestMapping("/importXls")
    @ResponseBody
    public JsonResult importXls(MultipartFile file) throws IOException {
        employeeService.importXls(file);
        return new JsonResult();

    }
}
